package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.service.ISearchUserinfoService;
import com.forlove.app.qselect.friend.service.impl.SearchUserinfoServiceImpl;
import com.forlove.app.qselect.friend.view.SearchUserinfoView;

import java.util.Map;

/**
 * Created by yjl on 2017/10/23.
 */

public class SearchUserinfoPresenter extends IBasePresenter {
    private SearchUserinfoView searchUserinfoView;
    private ISearchUserinfoService searchUserinfoService;

    public SearchUserinfoPresenter(SearchUserinfoView searchUserinfoView) {
        this.searchUserinfoView = searchUserinfoView;
        if (searchUserinfoService == null) {
            searchUserinfoService = new SearchUserinfoServiceImpl();
            setService(searchUserinfoService);
        }
    }

    public void loadSendRequestData(Map param) {
        searchUserinfoService.loadData(ServerManager.ACTION_FRIEND_ADD_REQUEST, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "添加好友申请:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (commonEntity.state == NetworkManager.ResultCode.RESULT_SUCCESS.getValue()) {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.sendAddRequestSuccess(NetworkManager.ResultCode.RESULT_SUCCESS, "发送好友请求成功!");
                    }
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (searchUserinfoView != null) {
                        searchUserinfoView
                                .showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });

    }


    /**
     * 短信邀请好友加入
     */
    public void loadInviteFriendBySms(Map param) {
        searchUserinfoService.loadData(ServerManager.ACTION_INVITE_FRIEND_BY_SMS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                searchUserinfoView.showInviteFriendView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void loadResetQuestionData(Map param) {
        searchUserinfoService.loadData(ServerManager.ACTION_RESET_QUESTION, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "重置答题:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                searchUserinfoView.showResetAnswerView(answerEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public void loadSearchFriend(Map param) {
        searchUserinfoService.loadData(ServerManager.ACTION_FRIEND_SEARCH, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                SearchFriendEntity searchFriendEntity = ParseHelper.getModel(result, "data", SearchFriendEntity.class);
                if (searchUserinfoView != null) {
                    searchUserinfoView.showSearchViewData(searchFriendEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (searchUserinfoView != null) {
                        searchUserinfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
